<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="500px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="户主姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入户主姓名"></el-input>
      </el-form-item>
      <el-form-item label="户主身份证号" prop="idcard">
        <el-input v-model="dataForm.idcard" placeholder="请输入户主身份证号"></el-input>
      </el-form-item>
      <el-form-item label="所属项目编号" prop="projectId">
        <el-input v-model="dataForm.projectId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="家庭人口" prop="familyNum">
        <el-input v-model="dataForm.familyNum" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="核准拆迁面积" prop="suqareCo">
        <el-input v-model="dataForm.suqareCo" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validateIdCard = (rule, value, callback) => {
        const reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[X])$)$/;
        if (value == '' || value == undefined || value == null) {
          callback();
        } else {
          if ((!reg.test(value)) && value != '') {
            callback(new Error('请输入正确的身份证号码'));
          } else {
            callback();
          }
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          idcard: '',
          projectId: '',
          familyNum: '',
          suqareCo: ''
        },
        dataRule: {
          name: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          idcard: [
            {required: true, message: '不能为空', trigger: 'blur'},
            { validator: validateIdCard, trigger: 'blur'}
          ],
          projectId: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          familyNum: [
            {required: false, message: '不能为空', trigger: 'blur'}
          ],
          suqareCo: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ator/family/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.family.name
                this.dataForm.idcard = data.family.idcard
                this.dataForm.projectId = data.family.projectId
                this.dataForm.familyNum = data.family.familyNum
                this.dataForm.suqareCo = data.family.suqareCo
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/family/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'idcard': this.dataForm.idcard,
                'projectId': this.dataForm.projectId,
                'familyNum': this.dataForm.familyNum,
                'suqareCo': this.dataForm.suqareCo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
